﻿$(function () {
    //初始化上传
    var tempRequestID = $('input[type="hidden"]#serviceRequestID').val();
    var tempID = $('input[type="hidden"]#iptTempID').val();
    if ($.trim(tempID) == '') {
        tempID = $('input[type="hidden"]#account_accountID').val();
    }
    $('#fileField').uploadify({

        'uploader': '/Content/images/uploadify.swf',
        'script': "/ITService/Upload",
        'scriptData': { 'tempRequestID': tempRequestID, 'tempID': tempID },
        'method': 'GET',
        'buttonText': '选择文件',
        'folder': 'UploadFiles',
        'fileTypeExts': '*.jpg;*.gif;*.png;*.txt;*.doc;*.docx;*.xls;*.xlsx;*.pdf;*.zip;*.rar;*.7z;*.pptx',
        'queueID': 'uploadFileQueue',
        'cancelImg': '/Content/Images/cancel.png',
        'sizeLimit': 1024 * 1024 * 4,
        'multi': false,
        'onComplete': callback,
        'onSelect': function (e, queueId, fileObj) {
            var fileSize = fileObj.size;
            var fileNameLength = fileObj.name.length;
            var fileEx = '*.jpg;*.gif;*.png;*.txt;*.doc;*.docx;*.xls;*.xlsx;*.pdf;*.zip;*.rar;*.7z;*.pptx;';
            if (fileSize >= (1024 * 1024 * 4)) {
                alert("您上传的文件大小超过限制，请重新上传！");
                $('#fileField').uploadifyCancel(id);
            }
            if (fileNameLength >= 45) {
                alert("您上传的文件名长度超过限制，请重新上传！");
                $('#fileField').uploadifyCancel(id);
            }
            if (fileEx.indexOf(fileObj.type.toLowerCase()+';') < 0) {
                alert("您上传的文件类型有误，请重新上传！");
                $('#fileField').uploadifyCancel(id);
            }
        }
    });
    function callback(event, queueID, fileObj, response, data) {
        if (response == "Exists") {
            alert("您所上传的文件已存在!");
        } else if (response != "") {
            var model = $.parseJSON(response);
            if (model.status == 1) {
                $("#tblAttachments tbody").append("<tr><td>" + model.fileName + "</td><td>" + model.fileSize + "</td><td>" + model.uploadDate + "</td><td>" + model.uploadUserName + "</td><td><a href='javascript:void(0)' id='" + model.requestid + "|" + model.fileName + "' class='delAttachment'>删除</a></td></tr>");
            } else {
                alert("上传附件失败，请稍后再试!");
            }
        } else {
            alert("上传附件失败，请稍后再试!");
        }
    }
    //删除附件
    $(".delAttachment").live("click", function () {
        if (confirm("您确定要删除该附件?")) {
            var fileData = this.id.split('|');
            if (fileData.length == 2) {
                var currColumn = $(this).parent().parent();
                $.ajax({
                    url: "delAttachment",
                    type: "POST",
                    dataType: "json",
                    data: { "folder": fileData[0], "fileName": fileData[1] },
                    success: function (data) {
                        if (data.status == 1) {
                            currColumn.remove();
                        } else {
                            alert("删除附件失败，请稍后再试!");
                        }
                    }
                });
            }
        } else {
            return false;
        }
    });
    //上传附件

    $("input[type='button'].uploadBtn").click(function () {
        if ($("#uploadFileQueue").children().length > 0) {
            if (confirm("您确定要上传该附件?")) {
                $('#fileField').uploadifyUpload();
            }
        }
        else {
            alert("您还未选择要上传的文件！");
        }

    });
    $("#backBtn").bind("click", function () {
        var entrylength = window.history.length;
        if (entrylength > 0)
            window.history.back();
        else
            window.location.href = "http://selfservice.fj-tobacco.com/WorkList/ScheduledWorkList";
    });
})